package com.lhstack.config.proxy.abstracts.impl;

import com.lhstack.config.proxy.abstracts.AbstractProxyStatement;
import lombok.SneakyThrows;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ProxyStatement extends AbstractProxyStatement {
    public ProxyStatement(Statement statement) {
        super(statement);
    }

    @Override
    @SneakyThrows
    public ResultSet executeQuery(String sql) throws SQLException {
        return (ResultSet) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .apply(() -> statement.executeQuery(sql));
    }

    @Override
    @SneakyThrows
    public int executeUpdate(String sql) throws SQLException {
        return (int) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .apply(() -> statement.executeUpdate(sql));
    }

    @Override
    @SneakyThrows
    public boolean execute(String sql) throws SQLException {
        return (boolean) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .apply(() -> statement.execute(sql));
    }

    @Override
    @SneakyThrows
    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        return (int) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .addMetadata("autoGeneratedKyes",autoGeneratedKeys)
                .apply(() -> statement.executeUpdate(sql,autoGeneratedKeys));
    }

    @Override
    @SneakyThrows
    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        return (int) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .addMetadata("columnIndexes",columnIndexes)
                .apply(() -> statement.executeUpdate(sql,columnIndexes));
    }

    @Override
    @SneakyThrows
    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        return (int) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .addMetadata("columnNames",columnNames)
                .apply(() -> statement.executeUpdate(sql,columnNames));
    }

    @Override
    @SneakyThrows
    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        return (boolean) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .addMetadata("autoGeneratedKeys",autoGeneratedKeys)
                .apply(() -> statement.execute(sql,autoGeneratedKeys));
    }

    @Override
    @SneakyThrows
    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        return (boolean) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .addMetadata("columnIndexes",columnIndexes)
                .apply(() -> statement.execute(sql,columnIndexes));
    }

    @Override
    @SneakyThrows
    public boolean execute(String sql, String[] columnNames) throws SQLException {
        return (boolean) obServerConsumer
                .buildMetaData()
                .addMetadata("statement",statement)
                .addMetadata("sql",sql)
                .addMetadata("columnNames",columnNames)
                .apply(() -> statement.execute(sql,columnNames));
    }
}
